/*
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef TIMER_H
#define TIMER_H

#include <plat/config.h>
#include <arch/cpu.h>
#include <arch/timers.h>
#include <stdlib/assert.h>

typedef struct ti__time {
    unsigned long time;
} time_t;

typedef struct time_data_t {
    time_t       boottime;
    unsigned int countdown;
} time_data_t;

extern time_data_t time_datas[];

void timer_datas_init(unsigned int core);
void timer_handler(void);
void timer_next(unsigned int us);

static inline time_data_t *get_time_data(unsigned int core)
{
    assert(core < CONFIG_SMP_CORES);
    return time_datas + core;
    ;
}

static inline time_t timer_current(void)
{
    unsigned int core = current_cpu();
    return get_time_data(core)->boottime;
}
static inline int timer_expired(time_t t, unsigned int core)
{
    return (t.time <= get_time_data(core)->boottime.time);
}
#endif
